import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '控制台', icon: 'dashboard' }
      },
      {
        path: 'changePwd',
        name: 'changePwd',
        hidden: true,
        component: () => import('@/views/login/changePwd'),
        meta: { title: '修改密码' }
      }
    ]
  },

  {
    path: '/goods',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('@/views/goods/index'),
        meta: { title: '商品管理', icon: 'el-icon-s-goods' }
      },
      {
        path: ':name',
        name: 'edit',
        hidden: true,
        component: () => import('@/views/goods/edit'),
        beforeEnter: (to, from, next) => {
          if (to.query.id) {
            to.meta.title = '编辑商品'
          } else {
            to.meta.title = '添加商品'
          }
          next()
        }
      }
    ]
  },

  {
    path: '/classify',
    component: Layout,
    children: [{
      path: 'index',
      name: 'classify',
      component: () => import('@/views/goods/classify'),
      meta: { title: '分类管理', icon: 'el-icon-menu' }
    }]
  },

  {
    path: '/banner',
    component: Layout,
    children: [{
      path: 'index',
      name: 'banner',
      component: () => import('@/views/goods/banner'),
      meta: { title: '轮播图片', icon: 'el-icon-picture' }
    }]
  },

  {
    path: '/order',
    component: Layout,
    children: [{
      path: 'index',
      name: 'index',
      component: () => import('@/views/order/index'),
      meta: { title: '订单管理', icon: 'el-icon-s-order' }
    }]
  },

  {
    path: '/vip',
    component: Layout,
    children: [{
      path: 'index',
      name: 'index',
      component: () => import('@/views/vip/index'),
      meta: { title: '用户管理', icon: 'el-icon-user-solid' }
    }]
  },

  {
    path: '/group',
    component: Layout,
    children: [{
      path: 'index',
      name: 'index',
      component: () => import('@/views/group/index'),
      meta: { title: '群聊管理', icon: 'el-icon-menu' }
    }]
  },

  {
    path: '/money',
    component: Layout,
    name: 'money',
    meta: { title: '资金管理', icon: 'el-icon-s-data' },
    children: [
      { path: 'record', component: () => import('@/views/money/record'), name: 'record', meta: { title: '充值记录' }},
      { path: 'balance', component: () => import('@/views/money/balance'), name: 'balance', meta: { title: '余额明细' }},
      { path: 'withdraw', component: () => import('@/views/money/withdraw'), name: 'withdraw', meta: { title: '提现管理' }}
    ]
  },

  {
    path: '/setting',
    component: Layout,
    children: [{
      path: 'index',
      name: 'index',
      component: () => import('@/views/config/index'),
      meta: { title: '全局设置', icon: 'el-icon-s-tools' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
